package com.urbansquall.chaos{
	
import flash.utils.*;

/* This class doesn't play nice with being modified after you've started cycling along it.
 * Calling .next will not allow any repeats until the whole cycle has been iterated, after which
 * the next cycle sequence is randomized. It is therefore possible to get the same option twice in a row
 * (if it is the last of this cycle and the first of the next), but it is impossible to get
 * it three times.
 */

public class RandomizedCycle
{
	private var m_queue : RandomizedArray;
	private var m_index : int;
	
	public function RandomizedCycle( a_random : PseudoRandom = null )
	{
		m_queue = new RandomizedArray( a_random );
		m_index = 0;
	}
	
	public function insert( a_object : Object ) : void
	{
		m_queue.insert( a_object );
	}
	
	// if all options have been used, the cycle is randomized and reset
	public function get next() : Object
	{
		var result : Object = m_queue.getObjectAt( m_index );
		m_index++;
		if( m_index >= m_queue.length )
		{
			m_index = 0;
			m_queue.randomize();
		}
		return result;
	}
}

}